package ngames.com.sushitv.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.RadioGroup;

import ngames.com.sushitv.Fragment.RegisterFragment;
import ngames.com.sushitv.R;
import ngames.com.sushitv.action.RegisterAction;
import ngames.com.sushitv.adapter.TabFragmentPagerAdapter;
import ngames.com.sushitv.base.BaseFragment;
import ngames.com.sushitv.base.BaseFragmentActivity;
import ngames.com.sushitv.tools.LogUtil;

public class RegisterActivity extends BaseFragmentActivity implements ViewPager.OnPageChangeListener {
    private BaseFragment mCurrentFragment;
    private RegisterFragment mEmailFragment;
    private RegisterFragment mPhoneFragment;
    private RadioGroup mTitleButton;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        String[] tabNames = new String[]{"E-mail", "Phone"};

        Fragment[] list = new Fragment[2];
        mEmailFragment = new RegisterFragment();
        Bundle args1 = new Bundle();
        args1.putString(RegisterFragment.BUNDLE_REGISTER_TYPE, RegisterFragment.REGISTER_TYPE_EMAIL);
        mEmailFragment.setArguments(args1);
        list[0] = mEmailFragment;
        mCurrentFragment = mEmailFragment;

        mPhoneFragment = new RegisterFragment();
        Bundle args2 = new Bundle();
        args2.putString(RegisterFragment.BUNDLE_REGISTER_TYPE, RegisterFragment.REGISTER_TYPE_PHONE);
        mPhoneFragment.setArguments(args2);
        list[1] = mPhoneFragment;

        TabFragmentPagerAdapter pagerAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), tabNames);
        pagerAdapter.setFragments(list);
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        mViewPager.setAdapter(pagerAdapter);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.addOnPageChangeListener(this);

        mTitleButton = (RadioGroup) findViewById(R.id.rdg_title_bar);
        mTitleButton.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rbt_email:
                        mViewPager.setCurrentItem(0);
                        break;
                    case R.id.rbt_phone:
                        mViewPager.setCurrentItem(1);
                        break;
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mEmailFragment.destroy();
        mPhoneFragment.destroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtil.d("mFBLoginAction", "RegisterActivity-onActivityResult() requestCode = " + requestCode);
        mCurrentFragment.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                mTitleButton.check(R.id.rbt_email);
                mCurrentFragment = mEmailFragment;
                break;
            case 1:
                mTitleButton.check(R.id.rbt_phone);
                mCurrentFragment = mPhoneFragment;
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public static void goRegisterActivity(Activity activity, String type) {
        Intent i = new Intent(activity, RegisterActivity.class);
        i.putExtra(RegisterAction.EXTRA_TYPE, type);
        activity.startActivity(i);
    }
}
